<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

require_once('utilidades.php');

function mesString($fecha) {
    $mes = date('m', $fecha);
    switch ($mes) {
        case 1: return "Enero";
        case 2: return "Febrero";
        case 3: return "Marzo";
        case 4: return "Abril";
        case 5: return "Mayo";
        case 6: return "Junio";
        case 7: return "Julio";
        case 8: return "Agosto";
        case 9: return "Septiembre";
        case 10: return "Octubre";
        case 11: return "Noviembre";
        case 12: return "Diciembre";
    }
}

function competicionesMes($fecha) {
    $mesActual = date('m', $fecha);
    $anioActual = date('Y', $fecha);
    $fechaSig = strtotime("+1 month", $fecha);
    $fechaAnt = strtotime("-1 month", $fecha);

    $resul['mesStr'] = mesString($fecha);
    $resul['mesAnt'] = "<a href=verCalendario.php?fecha=" . date("d-m-Y", $fechaAnt) . ">Mes Anterior</a><br>";
    $resul['mesSig'] = "<a href=verCalendario.php?fecha=" . date("d-m-Y", $fechaSig) . ">Mes Siguiente</a>";



//        print date('d-m-Y',strtotime("+1 m", $ahora));
//$mesSiguiente = date('m',strtotime("+1 m", $ahora));        
//        print "<br>" . date('d-m-Y',strtotime("+1 month", $ahora));

    $fechaini = date("Y-m-d", strtotime("1-" . $mesActual . "-" . $anioActual));
    $fechafin = date("Y-m-d", strtotime("1-" . date("m", $fechaSig) . "-" . date("Y", $fechaSig)));



    $sql = "select j.id as id,j.nombre,day(j.fecha) as fecha" .
            " from temporada t,competicion c, jornada j" .
            " where t.id=c.idtemporada" .
            " and c.id = j.idcompeticion" .
            " and j.fecha >='" . $fechaini . "'" .
            " and j.fecha<'" . $fechafin . "'" .
            "order by fecha asc";

//print $sql;

    $resul['filas'] = select2($sql);

    return $resul;
}

function tablaHTML($objeto, $array, $key) {
//print_r($array);
    $base = '../comun';
    print "<table border=1>";
    print "<tr>";

    if (count($array) > 0) {


        foreach ($array[0] as $columna => $valor) {
            print "<th>" . $columna . "</th>";
        }
        print "<th colspan=3></th>";
        print "</tr>";

        foreach ($array as $fila) {
            $uid = null;
            foreach ($fila as $columna => $valor) {
                if ($columna == $key) {
                    $uid = $valor;
                }
                print "<td>" . $valor . "</td>";
            }

//print_r($fila);
            $sql = "select count(*) as n" .
                    " from jornada j, rpruebajornada pj, serie s, resultado r" .
                    " where j.id=pj.idjornada" .
                    " and pj.id = s.idpruebajornada" .
                    " and s.id = r.idserie" .
                    " and j.id = " . $fila[$key];


            print "<td>" . "<a href=verHorarioJornada.php?idjornada=" . $fila[$key] . ">Horario</a></td>";
//print $sql;
            $resultados = select2($sql);
            //print_r($resultados);
            if ($resultados[0]['n'] > 0) {
                print "<td>" . "<a href=verResultadosJornada.php?idjornada=" . $fila[$key] . ">Resultados</a></td>";
//print "<td>" . "Hay resultados" . "Ver resultados</a></td>";
            } else {
                print "<td>-</td>";
            }

            print "</tr>";
        }

        print "</table>";
    } else {
        print "No hay registros";
    }
}

function tablaClubsHTML($objeto, $array, $key) {
//print_r($array);
    $base = '../comun';
    print "<table border=1>";
    print "<tr>";

    if (count($array) > 0) {


        foreach ($array[0] as $columna => $valor) {
            print "<th>" . $columna . "</th>";
        }
        print "<th colspan=3></th>";
        print "</tr>";

        foreach ($array as $fila) {
            $uid = null;
            foreach ($fila as $columna => $valor) {
                if ($columna == $key) {
                    $uid = $valor;
                }
                print "<td>" . $valor . "</td>";
            }
            print "<td><a href=verclub.php?id=" . $fila['id'] . ">Ver</a></td>";
            print "</tr>";
        }

        print "</table>";
    } else {
        print "No hay registros";
    }
}

function horarioJornada($objeto, $array, $key = 'id') {
//print_r($array);
    $base = '../comun';

    $html[] = "<h3> Horario </h3>";
    $html[] = "<table border=1>";
    $html[] = "<tr>";

    if (count($array) > 0) {

        $html[] = "<th>" . 'Hora' . "</th>";
        $html[] = "<th>" . 'Prueba' . "</th>";
        $html[] = "<th>" . 'Categoria' . "</th>";
        $html[] = "</tr>";

        foreach ($array as $fila) {
            $uid = null;
            $hora = $fila['hora'];
            $html[] = "<tr>";
            foreach ($fila as $columna => $valor) {

                if ($columna == $key)
                    continue;

                $html[] = "<td>" . $valor . "</td>";
            }
            $html[] = "</tr>";
        }
        $html[] = "</table>";
    } else {
        $html[] = "No hay registros";
    }

    return $html;
}

function head_consulta($req) {
    $time = time();
    $fecha = date('Y-m-d', $time);
    $sql = "select * from temporada where fechainicio<='" . $fecha . "' and fechafin>='" . $fecha . "'";
    //print $sql;
    session_start();

    if (isset($req['temporadaid'])) {
        $idtemporada = $req['temporadaid'];
        //$_SESSION['estado']['temporada']['id'];
        $sql = "select * from temporada where id=" . $idtemporada;
        $r = select2($sql);
        $_SESSION['estado']['temporada'] = $r[0];
    } 
    //session_destroy();    
    //print_r($_REQUEST);
    if (!isset($ses['estado']['temporada'])) {
        $temporada = select2($sql);
        $_SESSION['estado']['temporada'] = $temporada[0];
        //print_r($_SESSION);
        $sql = "select * from temporada order by fechainicio";
        $temporada = select2($sql);
        unset($_SESSION['temporadas']);
        foreach ($temporada as $tem) {
            $_SESSION['temporadas'][] = $tem;
        }
    }

    $actual = $_SERVER['REQUEST_SCHEME'] . "://" . $_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF'] . "?" . $_SERVER['QUERY_STRING'];


    print "<div>";
    print "<h1> Temporada " . $_SESSION['estado']['temporada']['nombre'] . "</h1>";
    print "<form name=temporada method=post action=" . $actual . ">";
    print "<select name='temporadaid'>";
    foreach ($_SESSION['temporadas'] as $tem) {
        print "<option value=" . $tem['id'] . ">" . $tem['nombre'] . "</option>";
    }

    print "</select>";
    print "<input type=submit value='Cambiar Temporada'/>";
    print "</form>";
    print "</div>";
}
?>

